<?php    
// session_start();
 if (empty($_SESSION['username']) AND empty($_SESSION['passuser'])){
  echo "<link href='style.css' rel='stylesheet' type='text/css'>
 <center>Untuk mengakses modul, Anda harus login <br>";
  echo "<a href=../../index.php><b>LOGIN</b></a></center>";
}
else{

function GetCheckboxes($table, $key, $Label, $Nilai='') {
  $s = "select * from $table order by nama_tag";
  $r = mysql_query($s);
  $_arrNilai = explode(',', $Nilai);
  $str = '';
  while ($w = mysql_fetch_array($r)) {
    $_ck = (array_search($w[$key], $_arrNilai) === false)? '' : 'checked';
    $str .= "<input type=checkbox name='".$key."[]' value='$w[$key]' $_ck>$w[$Label] ";
  }
  return $str;
}

$aksi="modul/mod_newsleter/aksi_newsleter.php";
switch($_GET['act']){
  // Tampil Berita
  default:
    echo "<h2>Management Newsleter</h2>
          <form method=get action='$_SERVER[PHP_SELF]'>
          <input type=hidden name=module value=berita>
          <div id=paging>Masukkan Judul Newsleter : <input type=text name='kata'><input type=submit class=tombol value=Cari></div>
          </form>
          <input type=button class=tombol value='Tambahkan Newsleter' onclick=\"window.location.href='?module=newsleter&act=tambahberita';\">";

    if (empty($_GET['kata'])){
    echo "<table>  
          <tr><th>No</th><th>Judul</th><th>Tgl. Buat</th><th>Tgl. Kirim</th><th>Aksi</th></tr>";

    $p      = new Paging;
    $batas  = 15;
    $posisi = $p->cariPosisi($batas);

    // if ($_SESSION['leveluser']=='admin'){
      $tampil = mysql_query("SELECT * FROM newsleter ORDER BY id DESC LIMIT $posisi,$batas");
    // }
    // else{
    //   $tampil=mysql_query("SELECT * FROM newsleter 
    //                        WHERE username='$_SESSION[namauser]'       
    //                        ORDER BY id_berita DESC LIMIT $posisi,$batas");
    // }
  
    $no = $posisi+1;
    while($r=mysql_fetch_array($tampil)){
      $tgl_buat=tgl_indo($r['buat']);
      $tgl_kirim=tgl_indo($r['kirim']);
      echo "<tr><td>$no</td>
                <td>$r[judul]</td>
                <td>$tgl_buat</td>
                <td>$tgl_kirim</td>
		            <td><a href=?module=newsleter&act=editberita&id=$r[id]><b>Edit</b></a> | 
		                <a href='$aksi?module=newsleter&act=hapus&id=$r[id]'><b>Hapus</b></a></td>
		        </tr>";
      $no++;
    }
    echo "</table>";

    // if ($_SESSION['leveluser']=='admin'){
      $jmldata = mysql_num_rows(mysql_query("SELECT * FROM newsleter"));
    // }
    // else{
    //   $jmldata = mysql_num_rows(mysql_query("SELECT * FROM newsleter WHERE username='$_SESSION[namauser]'"));
    // }  
    $jmlhalaman  = $p->jumlahHalaman($jmldata, $batas);
    $linkHalaman = $p->navHalaman($_GET['halaman'], $jmlhalaman);

    echo "<div id=paging>$linkHalaman</div><br>";
 
    break;    
    }
    else{
    echo "<table>  
          <tr><th>No</th><th>Judul</th><th>Tgl. Buat</th><th>Tgl. Kirim</th><th>Aksi</th></tr>";

    $p      = new Paging;
    $batas  = 15;
    $posisi = $p->cariPosisi($batas);

    // if ($_SESSION['leveluser']=='admin'){
      $tampil = mysql_query("SELECT * FROM newsleter WHERE judul LIKE '%$_GET[kata]%' ORDER BY id_berita DESC LIMIT $posisi,$batas");
    // }
    // else{
    //   $tampil=mysql_query("SELECT * FROM newsleter 
    //                        WHERE username='$_SESSION[namauser]'
    //                        AND judul LIKE '%$_GET[kata]%'       
    //                        ORDER BY id_berita DESC LIMIT $posisi,$batas");
    // }
  
    $no = $posisi+1;
    while($r=mysql_fetch_array($tampil)){
      $tgl_buat=tgl_indo($r['tgl_buat']);
      $tgl_kirim=tgl_indo($r['tgl_kirim']);
      echo "<tr><td>$no</td>
                <td>$r[judul]</td>
                <td>$tgl_buat</td>
                <td>$tgl_kirim</td>
		            <td><a href=?module=newsleter&act=editberita&id=$r[id]>Edit</a> | 
		                <a href='$aksi?module=newsleter&act=hapus&id=$r[id]'>Hapus</a></td>
		        </tr>";
      $no++;
    }
    echo "</table>";

    // if ($_SESSION['leveluser']=='admin'){
      $jmldata = mysql_num_rows(mysql_query("SELECT * FROM newsleter WHERE judul LIKE '%$_GET[kata]%'"));
    // }
    // else{
    //   $jmldata = mysql_num_rows(mysql_query("SELECT * FROM newsleter WHERE username='$_SESSION[namauser]' AND judul LIKE '%$_GET[kata]%'"));
    // }  
    $jmlhalaman  = $p->jumlahHalaman($jmldata, $batas);
    $linkHalaman = $p->navHalaman($_GET['halaman'], $jmlhalaman);

    echo "<div id=paging>$linkHalaman</div><br>";
 
    break;    
    }
  
  case "tambahberita":
    echo "<h2>Tambahkan Newsleter</h2>
          <form method=POST action='$aksi?module=newsleter&act=input' enctype='multipart/form-data'>
          <table>
          <tr><td width=70>Judul</td>     <td> : <input type=text name='judul' size=60></td></tr>
          ";
            
    echo "</td></tr>
          <tr><td>Isi Newsleter</td>  <td> <textarea name='isi_berita'  style='width: 600px; height: 350px;'></textarea></td></tr>";

    // $tag = mysql_query("SELECT * FROM tag ORDER BY tag_seo");
    // echo "<tr><td>Tag (Label)</td><td> ";
    // while ($t=mysql_fetch_array($tag)){
    //   echo "<input type=checkbox value='$t[tag_seo]' name=tag_seo[]>$t[nama_tag] ";
    // }
    
    echo "</td></tr>
          <tr><td colspan=2><input type=submit class=tombol value=Simpan>
                            <input type=button class=tombol value=Batal onclick=self.history.back()></td></tr>
          </table></form>";
     break;
    
    
  case "editberita":
    $edit = mysql_query("SELECT * FROM newsleter WHERE id='$_GET[id]'");
    $r    = mysql_fetch_array($edit);

    echo "<h2>Edit Newsleter</h2>
          <form method=POST enctype='multipart/form-data' action=$aksi?module=newsleter&act=update>
          <input type=hidden name=id value=$r[id]>
          <table>
          <tr><td width=70>Judul</td>     <td> : <input type=text name='judul' size=60 value='$r[judul]'></td></tr>
          "; 
          
    echo "</td></tr>
          <tr><td>Isi Newsleter</td>   <td> <textarea name='isi_berita' style='width: 600px; height: 350px;'>$r[isi_berita]</textarea></td></tr>
          ";
          
    // $d = GetCheckboxes('tag', 'tag_seo', 'nama_tag', $r[tag]);

    // echo "<tr><td>Tag (Label)</td><td> $d </td></tr>";
 
    echo  "<tr><td colspan=2><input type=submit class=tombol value=Update>
                            <input type=button class=tombol value=Batal onclick=self.history.back()></td></tr>
         </table></form>";
    break;  
}

}
?>

